package com.boxin.ims.modules.wechat.busi;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.momarketing.entity.GuestBook;
import com.boxin.ims.modules.momarketing.service.GuestBookService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * @author Jakemanse
 * @date 2013-10-24
 * @function <p>留言处理 </p>
 */
@Service
public class GuestBookProcessor extends BusinessProcessor{
	
	@Autowired
	private GuestBookService guestBookService;
	
	public GuestBookProcessor() {
		super();
	}

	@Override
	public ProcessMessage process() {
		
		WechatMember member = getMember();
		if(member == null) {
			member = new WechatMember();
		}
		member.setUser(user);
		member.setWechatId(openId);
//		if(member!=null){
			String keywords = textMessage.getContent();
			if("我的回复".equals(keywords)){//查询回复
				
				queryAndReply( member);
				return null;
			}else{
				
				String reply ="您的留言我们已记下，谢谢您的意见和建议,我们会尽快给您答复,请输入【我的回复】查看我们给您的回复内容.";
				GuestBook queryBook = new GuestBook();
				queryBook.setMember(member);
				queryBook.setOpenId(openId);
				queryBook.setStatus(GuestBook.STATUS_NORMAL);
				
				List bookList = guestBookService.find(queryBook);
				//每个人每天最多提5条建议
				if(bookList!=null && bookList.size()>5){
					reply = "您留言我们已经记录,今天您提的宝贵意见已经5条以上,非常感谢您的关心和支持,我们会尽快处理您的意见和建议,请输入【我的回复】查看答复内容.";
				}else{
					
					GuestBook book = new GuestBook();
					String content = textMessage.getContent();
					String leftMsg = content.replaceAll("留言", "");
					if(StringUtils.isEmpty(leftMsg)){
						reply="请输入您要留言的内容,回复格式如:【留言:我希望你们的优惠活动更多，更实惠!】";
					}else{
						
						book.setContent(leftMsg);
						book.setUser(user);
						book.setMember(member);
						book.setOpenId(openId);
						if(member.getId() == null){
							book.setMember(null);
						}
						guestBookService.save(book);
					}
				}
				writeToUser(reply);
				
			}
//		}
		
		
		return null;
	}

	/**
	 * @author Jakemanse
	 * @time 2013-10-25  上午9:09:27
	 * @function <p> 查询回复内容 </p>
	 */
	public void queryAndReply(WechatMember member){
		GuestBook guestBook = new GuestBook();
		guestBook.setStatus(GuestBook.STATUS_REPLING);
		guestBook.setMember(member);
		guestBook.setOpenId(openId);
		List<GuestBook> bookList =  guestBookService.find( guestBook);
		String reply = "亲，暂时未找到您的答复信息,谢谢您的关心和支持!";
		if(bookList!=null && !bookList.isEmpty()){		//有回复内容
			StringBuffer sb = new StringBuffer();
			int index = 0;
			
			for(GuestBook gb:bookList){
				sb.append("留言:").append(gb.getContent()).append("\r\n");
				sb.append("答复:").append(gb.getReply()).append("\r\n");
				if(index < bookList.size()-1)	sb.append("-------------------------------\r\n");
				
				//更新状态
				gb.setUpdateTime(new Date());
				gb.setStatus(GuestBook.STATUS_REPLIED);
				guestBookService.save(gb);
				index ++; 
				
			}
			reply = sb.toString();
		} 
		
		writeToUser(reply);
		
	}
	
	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}
	
	
}
